package com.example.Springsecurity.controller;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author ${崔灿}
 * @date 2023/01/02 18:26
 **/
@RestController
@RequestMapping("user")
public class UserController {
    @GetMapping("info")
    public Authentication info(){
        //获取security上下文对象
        SecurityContext securityContext = SecurityContextHolder.getContext();
        //用户的认证信息都封装到Authentication类中
        Authentication authentication = securityContext.getAuthentication();
        return authentication;
    }
    @GetMapping("delete")
    @PreAuthorize(value = "hasAnyAuthority('user:delete')")
    public String delete(){

        return "用户删除的操作";
    }

    @GetMapping("insert")
    @PreAuthorize(value = "hasAnyAuthority('user:insert')")
    public String insert(){

        return "用户添加的操作";
    }
    @GetMapping("update")
    @PreAuthorize(value = "hasAnyAuthority('user:update')")
    public String update(){

        return "用户修改的操作";
    }
    @GetMapping("query")
    @PreAuthorize(value = "hasAnyAuthority('user:query')")
    public String query(){

        return "用户查询的操作";
    }
    @GetMapping("export")
    @PreAuthorize(value = "hasAnyAuthority('user:export')")
    public String export(){

        return "用户导出的操作";
    }
}
